/**
 * Created with IntelliJ IDEA.
 * User: Katerina
 * Date: 15.01.13
 * Time: 3:21
 * To change this template use File | Settings | File Templates.
 */
package ru.gameChangers.homework.businessModels {

import flash.display.DisplayObject;
import flash.events.Event;

import mx.core.UIComponent;


public class Grid extends UIComponent {

    private var items:Array = new Array();
    private var statusLine:StatusLine;

    private var animatedItemI:int = 0;
    private var animatedItemJ:int = 0;



    public function Grid(width:int,  height:int) {

        var gridItemsWidth:int = width / 3 - ConfigurationConstants.GRID_INTERVALS;
        var gridItemsHeight:int = height /3 - ConfigurationConstants.GRID_INTERVALS;

        statusLine = new StatusLine(width);

        var scale:Number = (gridItemsWidth + 0.0) / ((new ConfigurationConstants.MODELS_TITLES[2]()).width + 0.0);


        var coefficientV:int = gridItemsWidth + ConfigurationConstants.GRID_INTERVALS;
        var coefficientH:int = gridItemsHeight + ConfigurationConstants.GRID_INTERVALS;
        for (var i:int = 0; i < 3; ++i) {
            addChild(processTitle(i, scale, coefficientH));
            addChild(processTitle(i, scale, coefficientV, true));

            items.push(new Array());
            for (var j:int = 0; j < 3; ++j) {
                var item:GridItem = new GridItem(this.x + j * coefficientV,
                        this.y + i * coefficientH, gridItemsWidth, gridItemsHeight, ConfigurationConstants.CELL_TITLES[i][j],
                        ContentDataConstants.getData(i,  j), statusLine);

                addChild(item);
                items[i].push(item);
            }
        }
        items[animatedItemI][animatedItemJ].startPlay();

        statusLine.x = 0;
        statusLine.y = height;
        addChild(statusLine);



        drawLines(gridItemsWidth, gridItemsHeight);

        addEventListener(Event.ENTER_FRAME, onEnterFrame);

    }

    private function onEnterFrame(e:Event):void {
        if (items[animatedItemI][animatedItemJ].play() == false) {
            ++animatedItemJ;
            if (animatedItemJ == 3) {
                animatedItemJ = 0;
                ++animatedItemI;
                if (animatedItemI == 3) {
                    animatedItemI = 0;
                }
            }
            items[animatedItemI][animatedItemJ].startPlay();
        }
    }

    private function drawLines(gridItemsWidth:int, gridItemsHeight:int) : void {
        var linePosition:int = gridItemsWidth + ConfigurationConstants.GRID_INTERVALS / 2;
        var lineLength:int = ConfigurationConstants.GRID_SIZE - ConfigurationConstants.GRID_INTERVALS + 1;
        graphics.lineStyle(1, ConfigurationConstants.LIGHT_LINE_COLOR);
        graphics.moveTo(linePosition, 0);
        graphics.lineTo(linePosition, lineLength);
        linePosition += ConfigurationConstants.GRID_INTERVALS + gridItemsWidth;
        graphics.moveTo(linePosition, 0);
        graphics.lineTo(linePosition, lineLength);

        linePosition = gridItemsHeight + ConfigurationConstants.GRID_INTERVALS / 2;
        graphics.moveTo(0, linePosition);
        graphics.lineTo(lineLength, linePosition);
        linePosition += ConfigurationConstants.GRID_INTERVALS + gridItemsHeight;
        graphics.moveTo(0, linePosition);
        graphics.lineTo(lineLength, linePosition);
    }

    private static function processTitle(idx:int, scale:Number, shift:int, isVertical:Boolean = false) : DisplayObject {
        var title:DisplayObject = new ConfigurationConstants.MODELS_TITLES[idx]();
        title.scaleX = -scale;
        title.scaleY = scale;

        var width:int = title.width;
        var height:int = title.height;
        if (isVertical) {
            title.rotation = -90;
            title.y = idx * shift + width;
            title.x = -height - (shift * 0.25);
        } else {
            title.x = idx * shift - width;
            title.y = -height - 7;
        }
        title.alpha = 0.9;
        return title;
    }


}
}
